﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">UrlLocator</title>
  <keywords>
    <keyword translate="true">IUrlLocator</keyword>
    <keyword translate="true">ResolveUrl</keyword>
    <keyword translate="true">UrlLocator</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">UrlLocator</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_IUrlLocator" styleclass="Normal" translate="true">IUrlLocator</link><text styleclass="Normal" translate="true"> class is responsible for resolving the right urls for the xaml views for a view model in navigation based applications. Before Catel 3.0, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Services_INavigationService" styleclass="Normal" translate="true">INavigationService</link><text styleclass="Normal" translate="true"> was responsible for resolving the url, but this responsibility is now taken over by the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_IUrlLocator" styleclass="Normal" style="font-style:normal;" translate="true">IUrlLocator</link><text styleclass="Normal" style="font-style:normal;" translate="true">.</text><text styleclass="Normal" translate="true"> The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Services_NavigationService" styleclass="Normal" translate="true">NavigationService</link><text styleclass="Normal" translate="true"> internally uses the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_IUrlLocator" styleclass="Normal" translate="true">IUrlLocator</link><text styleclass="Normal" translate="true"> to resolve the views.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Resolving by naming convention</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">It is possible to resolve </text><text styleclass="Normal" style="font-style:normal;" translate="true">views using the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_IUrlLocator" styleclass="Normal" style="font-style:normal;" translate="true">IUrlLocator</link><text styleclass="Normal" style="font-style:normal;" translate="true">. Then you can use the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_MVVM_IUrlLocator_ResolveUrl_2_4ab7a212" styleclass="Normal" style="font-style:normal;" translate="true">ResolveUrl</link><text styleclass="Normal" style="font-style:normal;" translate="true"> method to resolve the url based on the type of the view model.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">For example, the following view model:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">Catel.Examples.ViewModels.MyViewModel</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">will be resolved as:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">/Views/MyPage.xaml</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">Note that the UrlLocator checks whether the resource actually exists. If the resource does not exists, it will not be able to resolve a view</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Manually resolving a naming convention</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To manually resolve a naming convention, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">urlLocator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IUrlLocator&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var url = viewLocator.ResolveUrl(typeof(MyViewModel));</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Customizing naming conventions</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">By default, the</text><text styleclass="Normal" style="font-style:normal;" translate="true">&#32;</text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_IUrlLocator" styleclass="Normal" style="font-style:normal;" translate="true">IUrlLocator</link><text styleclass="Normal" style="font-style:normal;" translate="true"> uses the following naming conventions to resolve urls:</text></para>
    <list id="1" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Views/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Views/[VM]View.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Views/[VM]Control.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Views/[VM]Page.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Views/[VM]Window.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Controls/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Controls/[VM]Control.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Pages/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Pages/[VM]Page.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Windows/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/Windows/[VM]Window.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Views/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Views/[VM]View.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Views/[VM]Control.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Views/[VM]Page.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Views/[VM]Window.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Controls/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Controls/[VM]Control.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Pages/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Pages/[VM]Page.xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Windows/[VM].xaml</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">/UI.Windows/[VM]Window.xaml</text></li>
    </list>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">For more information, see </text><link displaytype="text" defaultstyle="true" type="topiclink" href="MVVM_Naming_conventions" styleclass="Notes" translate="true">naming conventions</link></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">However, it is possible to add or remove new naming conventions to support your own naming convention. For example, to add a new naming convention for a different assembly, use this code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">urlLocator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IUrlLocator&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">urlLocator.NamingConventions.Add(&quot;/MyPages/[VM]Page.xaml&quot;);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering custom urls</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Sometimes, a class doesn&apos;t follow a naming convention (for whatever reason possible). In such a case, it is possible to register a mapping manually using the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">urlLocator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.ResolveType&lt;IUrlLocator&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">urlLocator.Register(typeof(MyViewModelNotFollowingNamingConvention), &quot;/MyVerySpecialUrl.xaml&quot;);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Using a custom UrlLocator</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If you want to have total freedom to determine which url is provided per view model (maybe there are other services that have an impact on this), it is possible to create a custom</text><text styleclass="Normal" style="font-style:normal;" translate="true">&#32;</text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_IUrlLocator" styleclass="Normal" style="font-style:normal;" translate="true">IUrlLocator</link><text styleclass="Normal" style="font-style:normal;" translate="true"> implementation. Then the only thing to do is to register it using the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.Register&lt;IViewLocator, MyViewLocator&gt;();</text></para>
  </body>
</topic>
